#!/usr/bin/env python
# -*- coding: utf8 -*-

'''
Created on 2011-7-26

国美价格图片识别模块

@author: zhongfeng
'''

import ImageFilter, ImageChops
from captcha_price import *
from gome.gome_feature import GOME_FEATURES_MAP
import Image
import itertools
import re
import time

try:
    import psyco
    psyco.full()
except ImportError:
    pass

class CaptchaProfile_gome(CaptchaProfile):
    
    def __init__(self,features_map = GOME_FEATURES_MAP):
        super(CaptchaProfile_gome,self).__init__(features_map)
        
    def __new__(cls,features_map = GOME_FEATURES_MAP):
        return super(CaptchaProfile_gome, cls).__new__(cls,features_map)
    
    def filter(self,im_raw):
        pixdata_raw = im_raw.load()
        imge_size = im_raw.size
        im = Image.new('1',imge_size)
        xsize,ysize = imge_size
        pixdata = im.load()
        for x in xrange(xsize):
            for y in xrange(ysize):
                if pixdata_raw[x,y] == (0,0,0,0):
                    pixdata[x,y] = 255
                else:
                    pixdata[x,y] = 0
        return im
    
    def split(self, im,top = 6,bottom = 16):
        return super(CaptchaProfile_gome,self).split(im,top,bottom)

def captcha_gome(filename):
    return captcha(filename, CaptchaProfile_gome())
    
import os  
curModDir = os.path.dirname(os.path.abspath(__file__))
testFilePath = os.path.join(curModDir, 'test_resources')
if __name__ == '__main__':
    fileName = os.path.join(testFilePath, "0.png")
    print captcha_gome(fileName)
    im_raw = Image.open(fileName)
    pixdata_raw = im_raw.load()
    #r,g,b,a = im.split()
    im = im_raw.filter(ImageFilter.EDGE_ENHANCE_MORE).convert('L').convert('1')
    im = Image.new('1',im_raw.size)
    xsize,ysize = im.size
    pixdata = im.load()
    for x in xrange(xsize):
        for y in xrange(ysize):
            if pixdata_raw[x,y] == (0,0,0,0):
                pixdata[x,y] = 255
            else:
                pixdata[x,y] = 0
    print CaptchaImageAlgorithm.GetBinaryMap(im)
    
#    it1 = im.crop((3, 4, 13, 16))
#    print cia.GetBinaryMap(it1),'\n'
#    it2 = im.crop((15,4,24,16))
#    print cia.GetBinaryMap(it2)
#    print '+++++++++'
#    it2 = im.crop((25, 4, 34, 16))
#    it3 = im.crop ((36,4,45,16))
#    #it3 = im.crop((35, 4, 37, 16))
#    it4 = im.crop((38, 4, 47, 16))
#    it5 = im.crop((48, 4, 57, 16))
#    #it6 = im.crop((51, 3, 57, 11))
#    #it7 = im.crop((59, 3, 65, 11))
#    multilist = [[0 for col in range(5)] for row in range(3)]
#    print '\n'.join(( str(t) for t in multilist))
    #profile = CaptchaProfile_360Buy()
    
    #print captcha_360buy(r'c:\6.png')
        
        
